<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
abstract class Controller extends WdkController
{	
	protected $keywords = '';
	protected $description = '';
	function __construct()
	{
		parent::__construct();
		$this->config = Wdk::config();
		date_default_timezone_set($this->config['timezone']);
		
		$this->session = Wdk::helper('UserSession');
		
		$this->checkUrl();
		
		$this->tpl = new Template_();
		$this->tpl->compile_dir = CACHE_PATH.'tpl_/_compile';
		$this->tpl->template_dir = BASE_PATH;
		$this->tpl->cache_dir = CACHE_PATH.'tpl_/_cache';
		$this->uri = Wdk::helper('Uri');
		
		$this->layout = 'default';
	}
	private function checkUrl()
	{
		if($this->config['domain'] != $_SERVER['HTTP_HOST'])
		{
			parent::_redirect($_SERVER['REQUEST_URI']);
		}
	}
	
	protected function _view($data = array(), $file='')
	{
		$_PATH_ = $this->config['development'] ? "/app" : "/theme/{$this->config['theme']}";
		if( !$file)
		{
			$file = $_PATH_.$this->uri['path'].$this->uri['class'].'/'.$this->uri['method'].'.html';
		}
		$this->tpl->define('view', $file);
		$this->tpl->assign($data);
		$this->tpl->assign('config', $this->config);
		$this->tpl->assign('session', $this->session);
		$this->tpl->assign('path', dirname($file).'/');
		$view = $this->tpl->fetch('view');
		
		$this->tpl->define('_CONTENTS_', $_PATH_.'/layout.'.$this->layout.'.html');
		$this->tpl->assign('view', $view);
		$this->tpl->assign('PATH', $this->uri['path'].$this->uri['class'].'/');
		$output = $this->tpl->fetch('_CONTENTS_');
		parent::_parsing($output);
		print($output);
	}
	
	protected function _isLogin()
	{
		return $this->session->isLogin();
	}
	protected function _loginPage()
	{
		parent::_redirect('/user/login');
	}
}